package com.bite.demo.controller;


import com.bite.demo.constand.Constands;
import com.bite.demo.model.UserInfo;
import com.bite.demo.service.UserService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/login")
    public String login(String userName, String password, HttpSession session){
        log.info("用户登录 userName：{}，password{}",userName,password);
        //账号或密码为空
        if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)){
            return "用户名或密码不能为空";
        }
        UserInfo userInfo =  userService.queryUserByName(userName);
        if(userInfo == null || userInfo.getId() < 0 ) {
            return "用户不存在";
        }
        if(!password.equals(userInfo.getPassword())) {
            return "账号或密码错误";
        }
        //模拟验证数据, 账号密码正确 写死
/*
        if("zhangsan".equals(userName)|| "123456".equals(password)){
            return "账号或密码错误";
        }
*/

        //存储session
        session.setAttribute(Constands.USER_INFO_SESSION,userInfo);
        return "";
    }
}
